<?php
/** 
 * @author GhostWei
 * @date Oct 9, 2012
 */
class oauth_response_facebook extends oauth_api_response {
	
	public function getAvatar() {
		$un = $this->get('username');
		return "http://graph.facebook.com/$un/picture?type=large";
	}
	
	public function getNickname() {
		return $this->get('name', $this->get('username', $this->get('id')));
	}
	
	public function getGender() {
		if($this->get('gender') == 'male') {
			$gender = 1;
		} elseif($this->get('gender') == 'female') {
			$gender = 2;
		} else {
			$gender = 0;
		}
		return $gender;
	}
	
	public function getUid() {
		return $this->get('username', $this->get('id'));
	}

	public function getTweets() {
		$data = array();
		foreach($this->get('data', array()) AS $key => $row) {
			$data[] = $this->_contentRow($row);
		}
		return $data;
	}
	
	public function getUserTweetsPaginator() {
		$pagin = $this->get('paging');

		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$page = abs($page);
		if($page == 0) $page = 1;
		
		$_next = array();
		if(isset($pagin->next)) {
			parse_str($pagin->next, $_next);
		}

		$next = array(
				'page' => $page + 1,
				'until' => isset($_next['until']) ? $_next['until'] : time(),
		);
		if(isset($pagin->previous)) {
			parse_str($pagin->previous, $_prev);
		}
		$prev = array(
				'page' => $page - 1,
				'since' => isset($_prev['since']) ? $_prev['since'] : time(),
				'__previous' => isset($_prev['__previous']) ? $_prev['__previous'] : 1,
		);

		$pages = array();
		if($page <= 1) {
			$pages['Next'] = $next;
		} else {
			$pages['Previous'] = $prev;
			$pages['Next'] = $next;
		}
		$_page = $this->_pageSet();
		$_page->total = 0;
		$_page->page = $page;
		$_page->pages = $pages;
		return $_page;
	}
	
	public function getTopicTweetsPaginator() {
		return $this->getUserTweetsPaginator();
	}
	
	private function _contentRow($row) {
		$tweet = $this->_rowTweet();
		if(isset($row->from)) {
			$tweet->author->id = $row->from->id;
			$tweet->author->name = $row->from->name;
		}
		$tweet->author->location = '';
		$tweet->author->homepage = '';
		if(isset($row->from)) {
			$tweet->author->avatar = "http://graph.facebook.com/{$row->from->id}/picture?type=large";
		}
			
		$tweet->id = $row->id;
		
		$tweet->content = '';
		if(isset($row->name)) {
			if($row->link) {
				$tweet->content .= '[url=' . $row->link . ']' . $row->name . '[/url] ';
			} else {
				$tweet->content .= '#' . $row->name . '# ';
			}
		}
		if(isset($row->properties) && isset($row->properties->href)) {
			$tweet->content .= '[url=' . $row->properties->href . ']';
			if(isset($row->properties->text)) {
				$tweet->content .= $row->properties->text;
			} else {
				$tweet->content .= $row->properties->href;
			}
			$tweet->content .= '[/url]' . "\r\n";
		}
		if(isset($row->caption)) {
			$tweet->content .= $row->caption;
		}
		if(isset($row->type) && $row->type == 'status') {
			$tweet->content = isset($row->story) ? $row->story : '';
		} elseif(isset($row->message)) {
			$tweet->content = $row->message;
		}
		if(isset($row->description)) {
			$tweet->content .= "\r\n" . $row->description;
		}
		if(isset($row->story_tags)) {
			$_tags = array();
			foreach($row->story_tags AS $tags) {
				if(is_array($tags)) {
					foreach($tags AS $tag) {
						$_tags[] = '[@' . $tag->id . '|' . $tag->name . ']';
					}
				}
			}
			if(!empty($_tags)) {
				$tweet->content .= "\r\n " . implode(' ', $_tags) . ' ';
			}
		}
		if(isset($row->likes) && isset($row->likes->data)) {
			$tags = array();
			foreach($row->likes->data AS $tag) {
				foreach($tags AS $tag) {
					$_tags[] = '[@' . $tag->id . '|' . $tag->name . ']';
				}
			}
			if(!empty($_tags)) {
				$tweet->content .= "\r\n " . implode(' ', $_tags);
			}
		}
		$tweet->timeline = isset($row->created_time) ? strtotime($row->created_time) : time();
		if(isset($row->comments)) {
			$tweet->hot = $row->comments->count;
		}
		if(isset($row->picture)) {
			$tweet->images[] = array(
					'thumb' => $row->picture,
					'middle' => $row->picture,
					'original' => $row->picture,	
			);
		}
				
		if(isset($row->application) && isset($row->application->name)) {
			$view = system_view::getControllerActionViewInstance();
			$url = (string) $view->url('sns/topic', array('keyword' => $row->application->name));
			$tweet->source = array('name' => $row->application->name, 'url' => $url);
		} else {
			$tweet->source = array('name' => null, 'url' => null);
		}
		return $tweet;
	}

	public function getUser() {
		$user = $this->_rowAuthor();
		$user->id = $this->getUid();
		$user->name = $this->getNickname();
		$loc = $this->get('hometown');
		$user->location = isset($loc->name) ? $loc->name : 'unknown';
		$user->description = $this->get('bio');
		$user->homepage = $this->get('link');
		$user->avatar = $this->getAvatar();
		$user->gender = $this->getGender();
		$user->tweets = 0;
		$user->follows = 0;
		$user->following = 0;
		return $user;
	}
	
	public function getTopics() {
		$data = array();
		foreach($this->get('data', array()) AS $row) {
			$topic = $this->_rowTopic();
			$topic->name = str_replace('/', ' & ', $row->category);
			$topic->title = $row->category;
			$topic->hot = 0;
			$topic->description = $row->category;
			$data[] = $topic;
		}
		return $data;
	}
}
